package com.quanxi.qxaimain.dao.IndexDebugging;
/**
 * Author: caiyisen
 * Date: 2024/6/07
 * Time: 11:12
 * Package:com.quanxi.qxEmrForKn.controller.v2.IndexDebugging
 * Project:qxEmrForKn
 */


import com.quanxi.qxaimain.common.datasource.datasourceAOP.annotation.DataSource;
import com.quanxi.qxaimain.common.datasource.datasourceAOP.enums.DataSourceEnum;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface IndexDebuggingMapper {
    //增加
    @DataSource(DataSourceEnum.DB1)
    @Insert("<script>" +

            "insert into  ${table_name}(ind_id,ind_name,ind_def,sql_total,total_flag,is_detail,sql_detail,detail_flag,source,dept_name,model_name,sql_value,sql_detail_value,sql_time,sql_detail_time,admin_name,add_time,update_time) " +

            "values " +

            "(#{ind_id}, #{ind_name}, #{ind_def}, #{sql_total}, #{total_flag}, #{is_detail}, #{sql_detail}, #{detail_flag},  #{source}, #{dept_name}, #{model_name},#{sql_value},#{sql_detail_value},#{sql_time},#{sql_detail_time},#{admin_name},now(),now())" +



            "</script>")
    int sql_AddSqlNew( @Param("ind_id") String ind_id,
                       @Param("ind_name") String ind_name, @Param("ind_def") String ind_def,
                       @Param("sql_total") String sql_total, @Param("total_flag") int total_flag,
                       @Param("is_detail") int is_detail, @Param("sql_detail") String sql_detail, @Param("detail_flag") int detail_flag,
                       @Param("source") int source, @Param("dept_name") String dept_name, @Param("model_name") String model_name,
                       @Param("sql_value") String sql_value, @Param("sql_detail_value") String sql_detail_value, @Param("sql_time") String sql_time,
                       @Param("sql_detail_time") String sql_detail_time, @Param("admin_name") String admin_name, @Param("table_name") String table_name);

    @DataSource(DataSourceEnum.DB1)
    @Delete("delete  from ${table_name} where id=#{id} and admin_name=#{admin_name}")
    int  sql_Delete(@Param("id") int id, @Param("admin_name") String admin_name, @Param("table_name") String table);

//    @DataSource(DataSourceEnum.DB1)
//    @Update("update qxzsk_t_nlp_dic_emr_keword_inpatient_test_copy1 set keyword='ffff' where id =#{id}")
//    int sql_update(@Param(value = "sqlStr") String sqlStr);

    @DataSource(DataSourceEnum.DB1)
    @Update({
            "<script>",
            "UPDATE ${table_name}",
            "<set>",
            "<if test='id != null'>id = #{id},</if>",
            "<if test='ind_id != null'>ind_id = #{ind_id},</if>",
            "<if test='ind_name != null'>ind_name = #{ind_name},</if>",
            "<if test='ind_def != null'>ind_def = #{ind_def},</if>",
            "<if test='sql_total != null'>sql_total = #{sql_total},</if>",
            "<if test='total_flag != null'>total_flag = #{total_flag},</if>",
            "<if test='is_detail != null'>is_detail = #{is_detail},</if>",
            "<if test='sql_detail != null'>sql_detail = #{sql_detail},</if>",
            "<if test='detail_flag != null'>detail_flag = #{detail_flag},</if>",
            "<if test='source != null'>source = #{source},</if>",
            "<if test='dept_name != null'>dept_name = #{dept_name},</if>",
            "<if test='model_name != null'>model_name = #{model_name},</if>",
            "<if test='sql_value != null'>sql_value = #{sql_value},</if>",
            "<if test='sql_detail_value != null'>sql_detail_value = #{sql_detail_value},</if>",
            "<if test='sql_time != null'>sql_time = #{sql_time},</if>",
            "<if test='sql_detail_time != null'>sql_detail_time = #{sql_detail_time},</if>",
            "<if test='admin_name != null'>admin_name = #{admin_name},</if>",
            "update_time = now()",
            "</set>",
            "WHERE id = #{id} ",
            "</script>"
    })
    int sql_UpdateSqlNew (@Param("id") int id, @Param("ind_id") String ind_id,
                          @Param("ind_name") String ind_name, @Param("ind_def") String ind_def,
                          @Param("sql_total") String sql_total, @Param("total_flag") int total_flag,
                          @Param("is_detail") int is_detail, @Param("sql_detail") String sql_detail, @Param("detail_flag") int detail_flag,
                          @Param("source") int source, @Param("dept_name") String dept_name, @Param("model_name") String model_name,
                          @Param("sql_value") String sql_value, @Param("sql_detail_value") String sql_detail_value, @Param("sql_time") String sql_time,
                          @Param("sql_detail_time") String sql_detail_time, @Param("admin_name") String admin_name, @Param("table_name") String table_name);


    @DataSource(DataSourceEnum.DB1)
    @Select({"<script>",
            " select   add_time,\n" +
                    "            admin_name,\n" +
                    "            dept_name,\n" +
                    "            detail_flag,\n" +
                    "            id,\n" +
                    "            ind_def,\n" +
                    "            ind_id,\n" +
                    "            ind_name,\n" +
                    "            is_detail,\n" +
                    "            model_name,\n" +
                    "            source,\n" +
                    "            sql_detail,\n" +
                    "            sql_detail_time,\n" +
                    "            sql_detail_value,\n" +
                    "            sql_time,\n" +
                    "            sql_total,\n" +
                    "            sql_value,\n" +
                    "            status,\n" +
                    "            total_flag,\n" +
                    "            update_time,\n" +
                    "            '' backend_value,\n" +
                    "            '' application_db_value,\n" +
                    "            year from ${table_name}",
            "<where>",
            "<if test='ind_id_arr != null'>ind_id in (${ind_id_arr}) </if>",
            "<if test='ind_id_arr == null'> CONCAT_WS(id,ind_id,ind_name,ind_def,sql_total,total_flag,is_detail,sql_detail,detail_flag,source,dept_name,model_name,sql_value,sql_detail_value,sql_time,sql_detail_time,admin_name) like concat('%',#{keyWord},'%') or #{keyWord}=\"\"</if>",
            "</where>",
            "order by ${px_time} desc",
            "</script>"})
    List<Map<String, Object>> sql_Search(@Param("keyWord") String keyWord,@Param("table_name") String table_name,@Param("px_time") String px_time,@Param("ind_id_arr") String ind_id_arr,@Param("monthPeriod") String monthPeriod);


    @DataSource(DataSourceEnum.DB4)
    @Select("select distinct  KPI_RESULT  BACKEND_VALUE, KPI_CODE ind_id from QX_T_KPI_RESULT where DEPT_NAME='全院' and OUT_DATE_MONTH=#{monthPeriod} ")
    List<Map<String, String>> sql_Search1(@Param("monthPeriod") String monthPeriod);

    @DataSource(DataSourceEnum.DB2)
    @Select({ "<script>",
            "select distinct lyearvalue kpi_quota, dataid ind_id from qxqcs_t_kpi_quota_value ",
            "<where>",
            "<if test='str1 == str2'>   orgname = '全院' and year = SUBSTRING(#{monthPeriod}, 1,4)  and month =trim(leading '0' from  SUBSTRING(#{monthPeriod}, 6,2) ) and isdetail='1' </if>",
            "<if test='str1 != str2'>   orgname = '全院' and year = SUBSTRING(#{monthPeriod}, 1,4) and stage = CONCAT(trim(leading '0' from  SUBSTRING(#{monthPeriod}, 6,2)),'-',trim(leading '0' from  SUBSTRING(#{monthPeriod}, 14,2) )) and isdetail='1'  </if>",
            "</where>",
            "</script>"})
    List<Map<String, String>> sql_Search2_1(@Param("monthPeriod") String monthPeriod,@Param("str1") String str1,@Param("str2") String str2);
    @DataSource(DataSourceEnum.DB2)
    @Select({ "<script>",
            "select distinct lyearvalue kpi_key_quota,dataid ind_id  from qxqcs_t_kpi_key_quota_value ",
            "<where>",
            "<if test='str1 == str2'>   orgname = '全院' and year = SUBSTRING(#{monthPeriod}, 1,4)  and month =trim(leading '0' from  SUBSTRING(#{monthPeriod}, 6,2) ) and isdetail='1' </if>",
            "<if test='str1 != str2'>   orgname = '全院' and year = SUBSTRING(#{monthPeriod}, 1,4) and stage = CONCAT(trim(leading '0' from  SUBSTRING(#{monthPeriod}, 6,2)),'-',trim(leading '0' from  SUBSTRING(#{monthPeriod}, 14,2) )) and isdetail='1' </if>",
            "</where>",
            "</script>"})
    List<Map<String, String>> sql_Search2_2(@Param("monthPeriod") String monthPeriod,@Param("str1") String str1,@Param("str2") String str2);
    @DataSource(DataSourceEnum.DB2)
    @Select({ "<script>",
            "select distinct lyearvalue kpi_third_quot,dataid ind_id from qxqcs_t_kpi_third_quota_value ",
            "<where>",
            "<if test='str1 == str2'>   orgname = '全院' and year = SUBSTRING(#{monthPeriod}, 1,4)  and month =trim(leading '0' from  SUBSTRING(#{monthPeriod}, 6,2) ) and isdetail='1' </if>",
            "<if test='str1 != str2'>   orgname = '全院' and year = SUBSTRING(#{monthPeriod}, 1,4) and stage = CONCAT(trim(leading '0' from  SUBSTRING(#{monthPeriod}, 6,2)),'-',trim(leading '0' from  SUBSTRING(#{monthPeriod}, 14,2) )) and isdetail='1' </if>",
            "</where>",
            "</script>"})
    List<Map<String, String>> sql_Search2_3(@Param("monthPeriod") String monthPeriod,@Param("str1") String str1,@Param("str2") String str2);



//    @DataSource(DataSourceEnum.DB1)
//    @Select("select add_time,admin_name,dept_name,detail_flag,id,ind_def,ind_id,ind_name,is_detail,model_name,source,sql_detail,sql_detail_time,sql_detail_value,sql_time,sql_total,sql_value,status,total_flag,update_time,year  from ${table_name} where   \n" +
//            " (concat(id,ind_id,ind_name,ind_def,sql_total,total_flag,is_detail,sql_detail,detail_flag,source,dept_name,model_name,sql_value,sql_detail_value,sql_time,sql_detail_time,admin_name) like concat('%',#{keyWord},'%') or #{keyWord}=\"\")  order by ${px_time} desc")
//    List<Map<String, Object>> sql_Download(@Param("keyWord") String keyWord,@Param("table_name") String table_name,@Param("px_time") String px_time,@Param("monthPeriod") String monthPeriod);
}
